package cn.psh.org.service.impl;

import cn.psh.basic.service.impl.BaseServiceImpl;
import cn.psh.basic.util.MD5Utils;
import cn.psh.basic.util.StrUtils;
import cn.psh.org.domain.Employee;
import cn.psh.org.mapper.EmployeeMapper;
import cn.psh.org.service.IEmployeeService;
import cn.psh.user.domain.Logininfo;
import cn.psh.user.domain.User;
import cn.psh.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Transactional
@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Transactional
    @Override
    public void update(Employee employee) {
        //处理密码的严值
        String salt = StrUtils.getComplexRandomString(32);
        //对加盐后的密码进行加密
        String md5Pwd = MD5Utils.encrypByMd5(employee.getPassword() + salt);

        //设置加盐加密后的密码
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        //查以前的老数据：employee里面大的数据来自于前端  -就不会有logininfo_id,shop_id
        Employee oldEmployee = employeeMapper.queryById(employee.getId());//loginfo_id

        //修改user信息
        employee.setLogininfoId(oldEmployee.getLogininfoId());
        employee.setShopId(oldEmployee.getShopId());
        super.update(employee);

        System.out.println(oldEmployee.getLogininfoId());

        //修改登陆信息
        Logininfo oldLogininfo = logininfoMapper.queryById(oldEmployee.getLogininfoId());
        BeanUtils.copyProperties(employee,oldLogininfo);
        logininfoMapper.update(oldLogininfo);

        employeeMapper.delEmployeeRole(employee.getId());
        employeeMapper.saveEmployeeRole(employee);
    }
    @Transactional
    @Override
    public void save(Employee employee) {
        //        盐值
        String salt = StrUtils.getComplexRandomString(32);
        String md5pwd = MD5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5pwd);
//添加logininfo
        final Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee, logininfo);
        logininfo.setType(0);
        logininfo.setDisable(true);
        logininfoMapper.save(logininfo);
//        设置employee表中的logininfo_id添加
        employee.setLogininfoId(logininfo.getId());
        super.save(employee);
        employeeMapper.saveEmployeeRole(employee);
    }
    @Transactional
    @Override
    public void del(Long id) {
        final Employee employee = employeeMapper.queryById(id);
        logininfoMapper.del(employee.getLogininfoId());
        super.del(id);
        employeeMapper.delEmployeeRole(id);
    }
    @Transactional
    @Override
    public void batchDel(List<Long> ids) {
        for (Long id : ids) {
            del(id);
        }
    }

    @Override
    public List<Employee> listyg(Long id) {
        return  employeeMapper.listyg(id);

    }
}
